/*
 * Copyright 2008 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.splink.asbeangen.templates.as3;

import java.util.List;

import org.splink.asbeangen.templates.ClassTemplateHelper;
import org.splink.asbeangen.templates.IClassTemplate;
import org.splink.asbeangen.templates.ITemplateReaderClass;

public class AS3ReaderClass extends AS3Class implements ITemplateReaderClass {
	private String _dataTypename;
	private String _readMethod;

	public AS3ReaderClass(List<String> copyright, String packagename, String classname, List<String> interfazes,
			String ifazeBasePackage, IClassTemplate template) {
		super(copyright, packagename, ClassTemplateHelper.firstCharToUpperCase(classname) + DATA_CLASS_POSTFIX
				+ READER_CLASS_POSTFIX, interfazes, ifazeBasePackage, template);

		_dataTypename = classname + DATA_CLASS_POSTFIX;
		_imports += _template.getImport(ClassTemplateHelper.firstCharToLowerCaseArray(_packagename + "."
				+ _dataTypename));
		_methods = "";
		_readMethod = _template.getOpenReadMethod(_dataTypename);
		addToConstructor("xml", "XML", AS3Class.VAR_TYPE_NATIVE);
		addToMembers("xml", "XML", AS3Class.VAR_TYPE_NATIVE);
	}

	public void addToImports(String classpath) {
		String imp = _template.getImport(ClassTemplateHelper.firstCharToLowerCaseArray(classpath + DATA_CLASS_POSTFIX
				+ READER_CLASS_POSTFIX));

		if (!_imports.contains(imp)) {
			_imports += imp;
		}
	}

	public void addAttributeToReadMethod(String attribute, boolean handleExeption, String type) {
		String rMethod = _template.getReadAttribute(_dataTypename, attribute, handleExeption, type);

		if (!_readMethod.contains(rMethod)) {
			_readMethod += rMethod;
		}
	}

	public void addContentToReadMethod() {
		String readContent = _template.getReadContent(_dataTypename);
		if (!_readMethod.contains(readContent)) {
			_readMethod += readContent;
		}
	}

	public void addPcDataToReadMethod(String name, String type, boolean handleExeption) {
		String readPCData = _template.getReadPcData(name, _dataTypename, handleExeption);
		if (!_readMethod.contains(readPCData)) {
			_readMethod += readPCData;
		}
	}

	public void addSubelementToReadMethod(String subelement, boolean handleExeption) {
		String subReader = _template.getReadSubReader(_dataTypename, subelement, handleExeption);
		if (!_readMethod.contains(subReader)) {
			_readMethod += subReader;
		}
	}

	public void addSubelementArrayToReadMethod(String subelement, boolean handleExeption) {
		String subArReader = _template.getReadSubArReader(_dataTypename, subelement, handleExeption);
		if (!_readMethod.contains(subArReader)) {
			_readMethod += subArReader;
		}
	}

	public void addSubelementArrayMethod(String subelement) {
		String subArReader = _template.getReadSubArReaderMethod(subelement);
		if (!_readMethod.contains(subArReader)) {
			_methods += subArReader;
		}
	}

	public String getDefinition() {
		return _template.getComment(_copyright) + _definition + _imports + _clazz + _members
				+ ClassTemplateHelper.cleanLastOccurrence(_constructorHead, ",") + _constructorBody
				+ _template.getCloseConstructor() + _readMethod + _template.getClosePackMethod(_dataTypename)
				+ _methods + _template.getCloseClass() + _template.getClosePackage();
	}
}